package com.zucai365.football.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wcj.common.service.impl.WcjServiceImpl;
import com.zucai365.football.bean.FootballPlayerBean;
import com.zucai365.football.bean.FootballTeamBean;
import com.zucai365.football.bean.LianSaiBean;
import com.zucai365.football.bean.LianSaiYearBean;
import com.zucai365.football.bean.TeamPlayerBean;
import com.zucai365.football.dao.TeamPlayerDao;
import com.zucai365.football.service.TeamPlayerService;

@Service("teamPlayerService")
public class TeamPlayerServiceImpl extends WcjServiceImpl<TeamPlayerBean> implements TeamPlayerService{
	
	@Autowired
	@Qualifier("teamPlayerDao")
	private TeamPlayerDao teamPlayerDao;

	public TeamPlayerDao getTeamPlayerDao() {
		return teamPlayerDao;
	}

	public void setTeamPlayerDao( TeamPlayerDao teamPlayerDao ) {
		this.teamPlayerDao = teamPlayerDao;
	}

	@Override
	public void saveTeamPlayer( FootballTeamBean footballTeam,LianSaiBean liansai,LianSaiYearBean liansaiYear,
			List<FootballPlayerBean> footballPlayerList ) {
		
		List<TeamPlayerBean> teamPlayerList = new ArrayList<TeamPlayerBean>();
		for( FootballPlayerBean footballPlayer : footballPlayerList ){
			
			TeamPlayerBean teamPlayer = getTeamPlayer( liansaiYear.getId() , liansai.getId() , footballTeam.getId(), footballPlayer.getPlayerId() );
			if( teamPlayer == null ){
				teamPlayer = new TeamPlayerBean();
			}
			
			teamPlayer.setFootballPlayer(footballPlayer);
			teamPlayer.setLiansai(liansai);
			teamPlayer.setLiansaiYear(liansaiYear);
			teamPlayer.setFootballTeam(footballTeam);
			
			teamPlayer.setPlayerName( footballPlayer.getPlayerName() );
			teamPlayer.setFootballTeamName( footballTeam.getFootballTeamName() );
			teamPlayer.setJerseyNumber( footballPlayer.getJerseyNumber() );
			teamPlayer.setPosition( footballPlayer.getPosition() );
			
			String playerUrl = "http://info.bet007.com/cn/team/player.aspx?playerid="+ footballPlayer.getPlayerId() +"&TeamID="+footballTeam.getBet007Id();
			teamPlayer.setPlayerUrl( playerUrl );
			
			teamPlayerList.add( teamPlayer );
		}
		
		this.teamPlayerDao.saveAll( teamPlayerList );
	}

	@Override
	public TeamPlayerBean getTeamPlayer( Long lianSaiYearId, Long lianSaiId,
			Long footballTeamId, String playerId ) {

		DetachedCriteria criteria = DetachedCriteria.forClass( TeamPlayerBean.class, "teamPlayer" );
		criteria.createAlias( "teamPlayer.footballPlayer", "footballPlayer" );
		criteria.add( Restrictions.eq( "teamPlayer.liansaiYear.id", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "teamPlayer.liansai.id", lianSaiId ) );
		criteria.add( Restrictions.eq( "teamPlayer.footballTeam.id", footballTeamId ) );
		criteria.add( Restrictions.eq( "footballPlayer.playerId", playerId ) );
		
		List<TeamPlayerBean> list = this.teamPlayerDao.findByCriteria(criteria);
		
		if( list.size() > 1){
			throw new RuntimeException( "每个球队的球员有多个！" );
		}
		
		if( list.isEmpty() ){
			return null;
		}
		
		return list.get(0);
		
	}

	@Override
	public List<TeamPlayerBean> getTeamPlayerList(Long lianSaiYearId,
			Long lianSaiId, Long footballTeamId) {

		DetachedCriteria criteria = DetachedCriteria.forClass( TeamPlayerBean.class, "teamPlayer" );
		
		criteria.add( Restrictions.eq( "teamPlayer.liansaiYear.id", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "teamPlayer.liansai.id", lianSaiId ) );
		
		if( footballTeamId !=null ){
			criteria.add( Restrictions.eq( "teamPlayer.footballTeam.id", footballTeamId ) );
		}
		
		
		criteria.addOrder( Order.desc( "teamPlayer.position" ) );
		criteria.addOrder( Order.asc( "teamPlayer.jerseyNumber" ) );
		
		List<TeamPlayerBean> list = this.teamPlayerDao.findByCriteria(criteria);
				
		return list;
				
	}

	@Override
	public void saveTeamPlayerPlayTime( Long lianSaiId, Long lianSaiYearId ) {
		
		this.teamPlayerDao.saveTeamPlayerPlayTime(lianSaiId , lianSaiYearId );
		
	}

	@Override
	public void saveTeamPlayerHongHuangPai(Long lianSaiYearId, Long lianSaiId) {
		this.teamPlayerDao.saveTeamPlayerHongHuangPai(lianSaiYearId, lianSaiId);
	}
	
	/**
	 * 统计球员进球情况
	 * @param lianSaiYearId
	 * @param lianSaiId
	 */
	public void saveTeamPlayerJinQiu(  Long lianSaiYearId, Long lianSaiId ){
		this.teamPlayerDao.saveTeamPlayerJinQiu(lianSaiYearId, lianSaiId);
	}
}
